﻿@model  WaiGaoQiaoWeb.Models.DepartMentVM
@{
    ViewBag.Title = "DepartMentIndex.cshtml";
}
@section Head
{
    @Html.Css("main", "flexigrid", "tree")
    <script type="text/javascript">
        var maiheight = document.documentElement.clientHeight;
        $(document).ready(function () {
            var mainWidth = document.documentElement.clientWidth - 253; // 减去边框和左边的宽度
            var otherpm = 113;
            var gh = maiheight - otherpm;
            var option = {
                height: gh,
                width: mainWidth,
                url: '@Url.Action("GetDeparmentList")',
                dataType: 'json',
                colModel: [
                    { display: '部门ID', name: 'Dep_ID', width: 50, sortable: false, hide: true, align: 'left', iskey: true },
                    { display: '部门编号', name: 'GoodsNo', width: 80, sortable: false, hide: false, align: 'left', iskey: true },
                    { display: '部门名称', name: 'GoodsName', width: 200, sortable: false, hide: false, align: 'left', iskey: true },
					{ display: '上级ID', name: 'PriorNo', width: 80, sortable: false, align: 'left', iskey: true }
				],
                buttons: [
						    { name: 'Add', displayname: "新建", title: "新建部门", bclass: 'add InsertClass', onpress: toolbarItem_onclick },
							{ name: 'Edit', displayname: "修改", title: "修改部门", bclass: 'edit UpdateClass', onpress: toolbarItem_onclick },
                            { name: 'Delete', displayname: "删除", title: "删除部门", bclass: 'delete DeleteClass', onpress: toolbarItem_onclick },
							{ separator: true }
						 ],
                sortname: "DispOrder",
                sortorder: "asc",
                //height: 'auto', //flexigrid插件的高度，单位为px  
                //width: 'auto', //宽度值，auto表示根据每列的宽度自动计算  
                //sortorder: "asc",
                title: false,
                singleselected: false,
                usepager: true,
                page: 1, //默认当前页  
                total: 1, //总页面数  
                useRp: true, //是否可以动态设置每页显示的结果数  
                rp: 10, // 每页默认的结果数  
                rpOptions: [10, 15, 20], //可选择设定的每页结果数
                showcheckbox: true,
                selectedonclick: true,
                rowbinddata: true
            };

            var grid = $("#DepartmentList").flexigrid(option);


            $("#ptree").treeview({
                url: '@Url.Action("GetAllDepartMent")',
                showcheck: false,
                onnodeclick: TreeNode_Click,
                theme: "bbit-tree-no-lines" //bbit-tree-lines ,bbit-tree-no-lines,bbit-tree-arrows
            }).height(maiheight-30);

        });

        function refresh(parentId) {
            $("#DepartmentList").flexReload();

            $("#ptree").treeview({
                url: '@Url.Action("GetAllDepartMent")',
                onnodeclick: TreeNode_Click,
                showcheck: false,
                theme: "bbit-tree-no-lines"
            }).height(maiheight - 30);
        }


        // 触发树事件 
        function TreeNode_Click(data) {
            var p = { extParam: [{ name: "Key", value: data.id}] };
            $("#DepartmentList").flexOptions(p).flexReload();

            $("#PriorNo").val(data.id);
        }


        //新增，删除，修改
        function toolbarItem_onclick(cmd, grid) {
            if (cmd == "Add") {

                var id = $("#Dep_ID").val();
                var parentId = encodeURIComponent($("#PriorNo").val());
                OpenModelWindow('@Url.Action("DepartMentEdit")? Id=' + id + '&ParentId=' + parentId,
                    {
                        width: 350,
                        height: 250,
                        caption: "新增部门",
                        onclose: function () {
                            showErrorTip("操作成功！", { left: 400, top: 2 }, true, 3000);
                            refresh(parentId);
                        }
                    });
            }
            else if (cmd == "Edit") {
                if ($('.trSelected', grid).length == 0) {
                    showErrorTip("请选择要编辑的行！", { left: 400, top: 2 }, true, 3000);
                    return false;
                }

                if ($('.trSelected', grid).length > 1) {
                    showErrorTip("只能选择一行！", { left: 400, top: 2 }, true, 3000);
                    return false;
                }
                var parentId = encodeURIComponent($("#PriorNo").val());
                var items = $('.trSelected', grid);
                var Id = items[0].id.substr(3);
                OpenModelWindow('@Url.Action("DepartMentEdit")?Id=' + Id,
                                   { width: 350, height: 250, caption: "编辑部门",
                                       onclose: function () {
                                           showLoadingMsg("操作成功", { left: 400, top: 2 }, true, 3000);
                                           refresh(parentId);
                                       }
                                   });
            }
            else if (cmd = "Delete") {
                if ($('.trSelected', grid).length == 0) {
                    showErrorTip("请选择要删除的行！", { left: 400, top: 2 }, true, 3000);
                    return false;
                }

                var parentId = encodeURIComponent($("#PriorNo").val());
                if (confirm("确定要删除吗?")) {
                    var ids = "";
                    var items = $('.trSelected', grid);
                    for (i = 0; i < items.length; i++) {
                        ids = ids + ',' + items[i].id.substr(3);
                    } //for
                    $.ajax({
                        url: '@Url.Action("DeleteDepartment")',
                        type: "POST",
                        data: { DepartIds: ids },
                        success: function (callback) {
                            if (callback) {
                                if ((callback != true) && (callback != false)) {
                                    alert(callback);
                                    return;
                                }
                                showLoadingMsg("删除成功", { left: 400, top: 2 }, true, 3000);
                                refresh(parentId);
                            }
                        },
                        error: function () {
                            showErrorTip("操作失败！", { left: 400, top: 2 }, true, 3000);
                        }
                    }); // end $.ajax
                }
            }

        }
        
    </script>
}
<div id="container">
    <div id="bmsidebar">
        <div class="title">
            部门列表</div>
        <div id="ptree">
        </div>
    </div>
    <div id="bmcontent">
        <div id="title" class="title">
            部门管理</div>
        <table id="DepartmentList" style="display: none;">
        </table>
    </div>
    @Html.HiddenFor(model => model.Dep_ID)
    @Html.HiddenFor(model => model.PriorNo)
</div>
